package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geostar.constructionland.analyse.mapper.FxrzAlyMapper;
import com.geostar.constructionland.analyse.mapper.FxrzLsMapper;
import com.geostar.constructionland.analyse.service.fxrzstatistic.FxrzStatisticService;
import com.geostar.constructionland.common.entity.analyse.AlyFxrzLsPage;
import com.geostar.constructionland.common.entity.analyse.AlyFxrzPage;
import com.geostar.constructionland.common.entity.logs.JsydFxrz;
import com.geostar.constructionland.common.entity.logs.JsydFxrzLs;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * @author chengan
 * @description 分析日志展示界面controller
 */
@Controller
@RequestMapping("/analyseFxrz")
@RestController
@Api(value = "分析日志展示表")
public class AlyFxrzController {

    @Autowired
    FxrzAlyMapper fxrzAlyMapper;

    @Autowired
    FxrzStatisticService fxrzStatisticService;

    @Autowired
    FxrzLsMapper fxrzLsMapper;

    @PostMapping("/getFxrz")
    @ResponseBody
    @ApiOperation(value="获得真实分析日志")
    ExtJsAjaxResponse getFxrzResponse(@NotBlank(message = "页码不能为空")  @RequestParam(value = "page") int pageNo,
                                      @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
                                      String query_xmmc){
        IPage<JsydFxrz> jsydFxrzIPage = new Page<>(pageNo,pageNum);
        QueryWrapper<JsydFxrz> jsydFxrzQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(query_xmmc)){
            jsydFxrzQueryWrapper.like("xmmc",query_xmmc);
        }
        IPage<JsydFxrz> page = fxrzAlyMapper.queryFxrzPage(jsydFxrzIPage,jsydFxrzQueryWrapper.isNotNull("bz").orderByDesc("log_start_time"));
        ExtJsAjaxResponse extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(page);
        return extJsAjaxResponse;
    }

    @PostMapping("/getStatistic")
    @ResponseBody
    @ApiOperation(value="获得分析日志统计数据")
    ExtJsAjaxResponse getFxrzStatistics(@NotBlank(message = "页码不能为空")  @RequestParam(value = "page") int pageNo,
                                        @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
                                        String query_ywid){
        return  fxrzStatisticService.getFxrzStatisticList(pageNo,pageNum,query_ywid);
    }

    @PostMapping("/getFxrzByYwid")
    @ResponseBody
    @ApiOperation(value="通过ywid获得分析日志数据")
    ExtJsAjaxResponse getFxrzByYwid(String ywid){
        ExtJsAjaxResponse extJsAjaxResponse = null;
        QueryWrapper<JsydFxrz> jsydFxrzQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(ywid)){
            jsydFxrzQueryWrapper.eq("ywid",ywid);
            List<JsydFxrz> jsydFxrzs = fxrzAlyMapper.queryJsydFxrzByYwid(jsydFxrzQueryWrapper);
            //为了让前端接收数据的格式一样
            AlyFxrzPage alyFxrzPage = new AlyFxrzPage();
            alyFxrzPage.setRecords(jsydFxrzs);
            extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(alyFxrzPage);
        }else {
            //做分页查询，否则数据量过大，不能全传到前端
            IPage<JsydFxrz> jsydFxrzIPage = new Page<>(0,20);
            IPage<JsydFxrz> page = fxrzAlyMapper.queryFxrzPage(jsydFxrzIPage,jsydFxrzQueryWrapper);
            extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(page);
        }
        return  extJsAjaxResponse;
    }

    @PostMapping("/getFxrzLsByYwid")
    @ResponseBody
    @ApiOperation(value="通过ywid获得分析日志历史数据")
    ExtJsAjaxResponse getFxrzLsByYwid(String ywid){
        ExtJsAjaxResponse extJsAjaxResponse =null;
        QueryWrapper<JsydFxrzLs> queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(ywid)){
            queryWrapper.eq("ywid",ywid);
            List<JsydFxrzLs> jsydFxrzLs = fxrzLsMapper.getFxrzLsByYwid(queryWrapper);
            //为了让前端接收数据的格式一样
            AlyFxrzLsPage alyFxrzLsPage = new AlyFxrzLsPage();
            alyFxrzLsPage.setRecords(jsydFxrzLs);
            extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(alyFxrzLsPage);
        }else{
            extJsAjaxResponse=ExtJsAjaxResponse.createByErrorMessage("请传入ywid");
        }
        return extJsAjaxResponse;

    }
}
